package com.guihuo.mq.active;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 项目名称：middleware-learning
 * 类名称：ProducterMain
 * 类描述：ProducterMain
 * 创建时间：2019/1/10
 *
 * @author guihuo   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public class ProducterMain {

    public static void main(String[] args) {
        Producter producter = new Producter();
        producter.init();
        ProducterMain testMq = new ProducterMain();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ExecutorService executorService = Executors.newFixedThreadPool(5, new ThreadFactory() {

            private AtomicInteger count = new AtomicInteger();

            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("mq-producter-" + count.getAndIncrement());
                return thread;
            }

        });

        executorService.execute(new ProducterThread(producter));
        executorService.execute(new ProducterThread(producter));
        executorService.execute(new ProducterThread(producter));
        executorService.execute(new ProducterThread(producter));
        executorService.execute(new ProducterThread(producter));
    }

    public static class ProducterThread implements Runnable {

        private Producter producter;

        public ProducterThread(Producter producter) {
            this.producter = producter;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    producter.sendMessage("guihuo");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
